package com.ct.ah.lab.controller;

import com.ct.ah.lab.entity.Order;
import com.ct.ah.lab.entity.User;
import com.ct.ah.lab.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/orders")
public class OrderController {
    @Autowired
    private IOrderService orderService;

    @RequestMapping("/create")
    @ResponseBody
    public Map<String, Object> save(Integer aid, Integer[] cids, String infoParam, HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
//        User user = new User(2, "123456", "e10adc3949ba59abbe56e057f20f883e");
        if (user == null) {
            map.put("msg", "not login");
        } else {
            List<Integer> cidList = Arrays.asList(cids);
            Integer oid = orderService.save(aid, cidList, infoParam, user);
            //返回新增的这条订单数据
            Order order = orderService.getbyOid(oid);
            map.put("data", order);
            map.put("state", 1000);
        }
        return map;
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list(HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            map.put("msg", "not login");
        } else {
            List<Order> orders = orderService.list(user.getUid());
            map.put("state", 1000);
            map.put("data", orders);
        }
        return map;
    }

    //根据订单状态查询该用户所有的订单
    @RequestMapping("/listByStatus")
    @ResponseBody
    public Map<String, Object> listByStatus(Integer status, HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            map.put("msg", "not login");
        } else {
            List<Order> orders = orderService.listByStatus(user.getUid(), status);
            if (orders.size() > 0) {
                map.put("state", 1000);
                map.put("data", orders);
            } else {
                map.put("state", 2007);

            }
        }
        return map;
    }

    //根据订单id查询该用户所有的订单
    @RequestMapping("/getOrderByOid")
    @ResponseBody
    public Map<String, Object> getOrderByOid(Integer oid, HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            map.put("msg", "not login");
        } else {
            Order order = orderService.getbyOid(oid);
            System.out.println(order.toString());
            map.put("state", 1000);
            map.put("data", order);

        }
        return map;
    }


    //修改订单状态
    @RequestMapping("/changeStatus")
    @ResponseBody
    public Map<String, Object> changeStatus(Integer oid, Integer status, Integer payStyle, HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            map.put("msg", "not login");
        } else {
            orderService.changeStatus(oid, status, payStyle, user);
            map.put("state", 1000);
        }
        return map;
    }

    //退货
    @RequestMapping("/returnOrder")
    @ResponseBody
    public Map<String, Object> returnOrder(Integer oid, HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            map.put("msg", "not login");
        } else {
            orderService.returnOrder(oid, user);
            map.put("state", 1000);
        }
        return map;
    }


}
